package com.ys.nms.module.business.controller.admin.customOid;

import com.baomidou.dynamic.datasource.annotation.Slave;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.CommonResult;
import com.ys.nms.framework.common.util.object.BeanUtils;
import static com.ys.nms.framework.common.pojo.CommonResult.success;

import com.ys.nms.framework.excel.core.util.ExcelUtils;

import com.ys.nms.framework.apilog.core.annotation.ApiAccessLog;
import static com.ys.nms.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ys.nms.module.business.controller.admin.customOid.vo.*;
import com.ys.nms.module.business.dal.dataobject.customOid.CustomOidDO;
import com.ys.nms.module.business.service.customOid.CustomOidService;

@Tag(name = "管理后台 - 自定义OID")
@RestController
@RequestMapping("/business/custom-oid")
@Validated
public class CustomOidController {

    @Resource
    private CustomOidService customOidService;

    @Slave
    @PostMapping("/create")
    @Operation(summary = "创建自定义OID")
    public CommonResult<Integer> createCustomOid(@Valid @RequestBody CustomOidSaveReqVO createReqVO) {
        return success(customOidService.createCustomOid(createReqVO));
    }

    @Slave
    @PutMapping("/update")
    @Operation(summary = "更新自定义OID")
    public CommonResult<Boolean> updateCustomOid(@Valid @RequestBody CustomOidSaveReqVO updateReqVO) {
        customOidService.updateCustomOid(updateReqVO);
        return success(true);
    }

    @Slave
    @DeleteMapping("/delete")
    @Operation(summary = "删除自定义OID")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteCustomOid(@RequestParam("id") Integer id) {
        customOidService.deleteCustomOid(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除自定义OID")
    public CommonResult<Boolean> deleteCustomOidList(@RequestParam("ids") List<Integer> ids) {
        customOidService.deleteCustomOidListByIds(ids);
        return success(true);
    }

    @Slave
    @GetMapping("/get")
    @Operation(summary = "获得自定义OID")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CustomOidRespVO> getCustomOid(@RequestParam("id") Integer id) {
        CustomOidDO customOid = customOidService.getCustomOid(id);
        return success(BeanUtils.toBean(customOid, CustomOidRespVO.class));
    }

    @Slave
    @GetMapping("/page")
    @Operation(summary = "获得自定义OID分页")
    public CommonResult<PageResult<CustomOidRespVO>> getCustomOidPage(@Valid CustomOidPageReqVO pageReqVO) {
        PageResult<CustomOidDO> pageResult = customOidService.getCustomOidPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CustomOidRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出自定义OID Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCustomOidExcel(@Valid CustomOidPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomOidDO> list = customOidService.getCustomOidPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "自定义OID.xls", "数据", CustomOidRespVO.class,
                        BeanUtils.toBean(list, CustomOidRespVO.class));
    }

}